from typing import AsyncIterable

from fastapi import APIRouter, Body, File, UploadFile
from fastapi.responses import StreamingResponse
from .models import RecognitionRequest, RecognitionResponse
from .handlers import handle_recognition_request, stream_recognition_response

router = APIRouter()

@router.get("/recognize", response_model=RecognitionResponse)
async def recognize(request_data: RecognitionRequest):
    """
    处理图像、视频或流的识别请求。
    """
    return await handle_recognition_request(request_data)

@router.get("/recognize/stream", response_class=StreamingResponse)
async def recognize_stream(request_data: RecognitionRequest):
    """
    处理图像、视频或流的识别请求，并将结果作为流响应返回。
    """
    processed_result = await handle_recognition_request(request_data)
    if isinstance(processed_result, AsyncIterable):
        return await stream_recognition_response(request_data.request_id, processed_result)
    else:
        raise ValueError("Processed result must be an AsyncIterable to use stream-based response.")
